package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.model.Account;
import com.asset.salesforce.model.AccountData;
import com.asset.salesforce.model.AccountList;
import com.asset.salesforce.model.City;
import com.asset.salesforce.model.Country;
import com.asset.salesforce.model.Employee;
import com.asset.salesforce.model.LeadDetails;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;

@Controller
public class AccountController {

	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private CityService cityService;
	@Autowired
	private CountryService countryService;
	
//	@RequestMapping(value="/getAllAccounts/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
//	@ResponseBody
//	public List<AccountData> getAllLeads(@PathVariable String empId){
//		List<Account> accounts = accountService.getAllAccountByEmpId(Integer.parseInt(empId));
//		List<AccountData>accountData = new ArrayList<AccountData>();
//		for (Account account : accounts) {
//			AccountData data = new AccountData();
//			Employee employee = employeeService.getById(account.getAccountCreator().getId());
//			data.setAccountCreatorName(employee.getfName());
//			data.setAccountName(account.getAccountName());
//			data.setCompanyName(account.getCompanyName());
//			accountData.add(data);
//		}
//		return accountData;
//	}
	@RequestMapping(value="/getAllAccounts/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public AccountList getAllLeads(@PathVariable String empId){
		List<Account>accounts = accountService.getAllAccountByEmpId(Integer.parseInt(empId));
		AccountList accountList = new AccountList();
		List<AccountData>accountData = new ArrayList<AccountData>();
		for (Account account : accounts) {
			AccountData data = new AccountData();
			data.setId(account.getId());
			data.setCompanyName(account.getCompanyName());
			data.setAccountName(account.getAccountName());
			if(account.getEmail()!=null&&!account.getEmail().equalsIgnoreCase("")){
				data.setEmail(account.getEmail());
			}
			String phoneMobile = null ;
			if(account.getMobile()!=null&&!account.getMobile().equalsIgnoreCase("")){
				phoneMobile=account.getMobile();
			}
			if(account.getPhone()!=null&&!account.getPhone().equalsIgnoreCase("")){
				phoneMobile+=", "+account.getPhone();
			}
			if(phoneMobile!=null){
				data.setMobile(phoneMobile);
			}
			Employee employee = employeeService.getById(account.getAccountCreator().getId());
			data.setAccountCreatorName(employee.getfName()+" "+employee.getlName());
			accountData.add(data);
		}
		accountList.setAccountData(accountData);
		return accountList;
	}
	@RequestMapping(value="/getAccount/{id}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public Account getAccount(@PathVariable String id){
		Account account = accountService.getAccount(Integer.parseInt(id));
		Employee employee = employeeService.getById(account.getAccountCreator().getId());
		if(employee!=null){
			account.setAccountCreatorName(employee.getfName()+" "+employee.getlName());
		}
		if(account.getCity()!=null){
			City city = cityService.getCityById(account.getCity().getId());
			account.setCityName(city.getName());
		}
		if(account.getCountry()!=null){
			Country country = countryService.getCountryById(account.getCountry().getId());
			account.setCountryName(country.getCountryName());
		}
		return account;
	}
	
	@RequestMapping(value="/addAccountGet/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public LeadDetails addAccountGet(@PathVariable String empId){
		Employee employee = employeeService.getById(Integer.parseInt(empId));
		List<Country>countries = countryService.getAll();
		//List<City>cities = cityService.getAllCities();
		LeadDetails lead = new LeadDetails();
		if(employee!=null){
			lead.setOwnerName(employee.getfName()+" "+employee.getlName());
		}
		lead.setCountries(countries);
		return lead;
	}
	
	@RequestMapping(value="/addAccount",method=RequestMethod.POST,consumes="application/json;charset=UTF-8")
	@ResponseBody
	public Account addAccount(@RequestBody Account account){
		System.out.println("sucess");
		if(account.getAccountCreatorId()!=null){
			Employee accountOwner = employeeService.getById(account.getAccountCreatorId());
			account.setAccountCreator(accountOwner);
		}
		if(account.getCountryId()!=null){
			Country country = countryService.getCountryById(account.getCountryId());
			account.setCountry(country);
		}
		if(account.getCityId()!=0){
			City city = cityService.getCityById(account.getCityId());
			account.setCity(city);
		}
		accountService.addAccount(account);
		return account;
	}
}
